def hour_cut(x):
    if 0 <= x <= 7:  # 凌晨
        return [1, 0, 0, 0, 0]
    elif 7 < x <= 12:  # 上午
        return [0, 1, 0, 0, 0]
    elif 12 < x <= 17:  # 下午
        return [0, 0, 1, 0, 0]
    elif 17 < x <= 19:  # 傍晚
        return [0, 0, 0, 1, 0]
    elif 19 < x < 24:  # 晚上
        return [0, 0, 0, 0, 1]


def round_and_none_neg(n):
    if n < 0:
        return 0
    else:
        return round(n)


def holiday(x):
    if 0 <= x <= 4:
        return 0
    else:
        return 1


def median(lst):
    lst.sort()
    length = len(lst)
    mid = length // 2
    if length % 2 == 0:
        return (lst[mid - 1] + lst[mid]) / 2
    else:
        return lst[mid]


def mean(lst):
    if len(lst) == 0:
        return 0
    return sum(lst) / len(lst)


def std(lst):
    if len(lst) < 2:
        return 0
    return 0


import jieba


def content_data(word_data, content):
    words = jieba.lcut(content)
    words = [word for word in words if len(word) > 1]

    f_min = []
    f_max = []
    f_median = []
    f_std = []
    f_mean = []
    c_min = []
    c_max = []
    c_median = []
    c_std = []
    c_mean = []
    l_min = []
    l_max = []
    l_median = []
    l_std = []
    l_mean = []
    for word in words:
        if word not in word_data.keys():
            continue

        f_min.append(word_data[word]["f_min"])
        l_min.append(word_data[word]["l_min"])
        c_min.append(word_data[word]["c_min"])

        f_max.append(word_data[word]["f_max"])
        c_max.append(word_data[word]["c_max"])
        l_max.append(word_data[word]["l_max"])

        f_median.append(word_data[word]["f_median"])
        c_median.append(word_data[word]["c_median"])
        l_median.append(word_data[word]["l_median"])

        f_std.append(word_data[word]["f_std"])
        c_std.append(word_data[word]["c_std"])
        l_std.append(word_data[word]["l_std"])

        f_mean.append(word_data[word]["f_mean"])
        c_mean.append(word_data[word]["c_mean"])
        l_mean.append(word_data[word]["l_mean"])

    return (
        mean(f_min),
        mean(f_max),
        mean(f_median),
        mean(f_std),
        mean(f_mean),
        mean(c_min),
        mean(c_max),
        mean(c_median),
        mean(c_std),
        mean(c_mean),
        mean(l_min),
        mean(l_max),
        mean(l_median),
        mean(l_std),
        mean(l_mean),
    )


def user_data(user_state, uid):
    if uid in user_state.keys():
        return user_state[uid]
    else:
        return {
            "forward_min": 0,
            "forward_max": 0,
            "forward_median": 0,
            "forward_std": 0,
            "forward_mean": 0,
            "comment_min": 0,
            "comment_max": 0,
            "comment_median": 0,
            "comment_std": 0,
            "comment_mean": 0,
            "like_min": 0,
            "like_max": 0,
            "like_median": 0,
            "like_std": 0,
            "like_mean": 0,
        }


def user_data_from_json(json_data):
    forword_data = [x["forward_count"] for x in json_data]
    comment_data = [x["comment_count"] for x in json_data]
    like_data = [x["like_count"] for x in json_data]
    return [
        min(forword_data),
        max(forword_data),
        median(forword_data),
        0,
        mean(forword_data),
        min(comment_data),
        max(comment_data),
        median(comment_data),
        mean(comment_data),
        0,
        min(like_data),
        max(like_data),
        median(like_data),
        0,
        mean(like_data),
    ]
